package com.example.pv.handlerthread;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {

    private Button bt1 = null;
    private Button bt2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button bt1 = (Button)findViewById(R.id.bt_start);
        Button bt2 = (Button)findViewById(R.id.bt_end);
        bt1.setOnClickListener(new startListener());
        bt2.setOnClickListener(new endListener());
    }

    Handler handler = new Handler();

   class startListener implements OnClickListener{
       @Override
       public void onClick(View view) {
        handler.post(updateThread);
       }
   }

    class endListener implements OnClickListener{
        @Override
        public void onClick(View view) {
            handler.removeCallbacks(updateThread);
        }
    }

    Runnable updateThread =  new Runnable(){
        @Override
        public void run() {
            System.out.println("Update");
            Log.v("tag", "Update");
            handler.postDelayed(updateThread, 2000);
        }
    };
}
